package com.av.record.views;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public abstract class VOJSBCBaseView extends FrameLayout implements View.OnClickListener {

    protected View rootView;

    public VOJSBCBaseView(@NonNull Context context) {
        super(context);
        initViews();
        setClickAction();
    }

    public VOJSBCBaseView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews();
        setClickAction();
    }

    public VOJSBCBaseView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews();
        setClickAction();
    }

    protected void initViews(){
        rootView= LayoutInflater.from(getContext()).inflate(Res.getLayoutID(getLayoutId()),this);
    }

    public abstract String getLayoutId();

    protected void setAdvertSize(int width,int height){}

    protected void setClickAction(){}

    protected  <T extends View> T getViewById(View view, String id){
        return view.findViewById( Res.getWidgetID(id));
    }

    protected  <T extends View> T getViewById(String id){
        return rootView.findViewById( Res.getWidgetID(id));
    }

    @Override
    public void onClick(View v) {

    }

    public Drawable getDrawableRes(String drawableRes){
        Drawable drawable=getResources()
                .getDrawable(Res.getMipMapID(drawableRes));
        drawable.setBounds(0,0,
                drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
        return drawable;
    }

    /**
     * 消息
     * @param msg  消息
     */
    public void sendNewComment(String msg){

    }

    public void setHtmlText(TextView view,String text){
        if(TextUtils.isEmpty(text)){
            view.setText("");
        }else {
            view.setText(Html.fromHtml(text));
        }
    }

    public void toastMsg(String msg){
        Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
    }

    public void setViewSize(View view,int standW,int standH,int width,int heigth){
        ViewGroup.LayoutParams params=view.getLayoutParams();
        if(width<=0 && heigth<=0){
            params.width=standW;
            params.height=standH;
        }else {
            int w=standW;
            int h=w*heigth/width;
            if(h>standH){
                h=standH;
                w=h*width/heigth;
            }
            params.width=w;
            params.height=h;
        }
        view.setLayoutParams(params);
    }
}
